Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 轮播图的图片
    swiperItem: [{
      id: 1,
      url: "../../static/swiper/swiper1.jpg"
    }, {
      id: 2,
      url: "../../static/swiper/swiper2.jpg"
    }, {
      id: 3,
      url: "../../static/swiper/swiper3.jpg"
    }, {
      id: 4,
      url: "../../static/swiper/swiper4.jpg"
    }, ],
    // 首页下面的推荐项信息
    recommendList: [{
      id: 1,
      imaUrl: "../../static/recommend/推荐1.jpg",
      disc: "灰棕色优雅格子裙 年轻时尚 明星同款",
      price: 234,
      collect: 3.4,
      selected: false,
      total: 0
    }, {
      id: 2,
      imaUrl: "../../static/recommend/推荐2.jpg",
      disc: "金色艺术风桌面摆件 唯美 办公 艺术 ",
      price: 342,
      collect: 2.6,
      selected: false,
      total: 0
    }, {
      id: 3,
      imaUrl: "../../static/recommend/推荐3.jpg",
      disc: "优雅孔雀绿点花欧美风连衣裙 优雅 ins风",
      price: 123,
      collect: 7.1,
      selected: false,
      total: 0
    }, {
      id: 4,
      imaUrl: "../../static/recommend/推荐4.jpg",
      disc: "红色猛男运动卫衣 冬季 保暖",
      price: 299,
      collect: 10,
      selected: false,
      total: 0
    }, {
      id: 5,
      imaUrl: "../../static/recommend/推荐5.jpg",
      disc: "成熟妈妈风红色衬衣 独立女性 美丽 中国红",
      price: 376,
      collect: 8,
      selected: false,
      total: 0
    }, {
      id: 6,
      imaUrl: "../../static/recommend/推荐6.jpg",
      disc: "学生优雅白色青春半透衬衣 百搭",
      price: 877,
      collect: 3,
      selected: false,
      total: 0
    }, {
      id: 7,
      imaUrl: "../../static/recommend/推荐7.jpg",
      disc: "显锁骨开肩衬衣 名媛最爱 美丽优雅大方",
      price: 999,
      collect: 7,
      selected: false,
      total: 0
    }, {
      id: 8,
      imaUrl: "../../static/recommend/推荐8.jpg",
      disc: "法国时装转最新时装款式 连衣裙办公西装外搭风",
      price: 490,
      collect: 8.8,
      selected: false,
      total: 0
    }, {
      id: 9,
      imaUrl: "../../static/recommend/推荐9.jpg",
      disc: "儿童运动跑鞋 护脚不伤腿",
      price: 850,
      collect: 12,
      selected: false,
      total: 0
    }, {
      id: 10,
      imaUrl: "../../static/recommend/推荐10.jpg",
      disc: "白色超级跑鞋 纳米防污明星同款",
      price: 600,
      collect: 12.3,
      selected: false,
      total: 0
    }, {
      id: 11,
      imaUrl: "../../static/recommend/推荐11.jpg",
      disc: "时尚黑白格灯绒外套",
      price: 700,
      collect: 6.1,
      selected: false,
      total: 0
    }, {
      id: 12,
      imaUrl: "../../static/recommend/推荐12.jpg",
      disc: "帅气儿童淡蓝夏季运动装",
      price: 721,
      collect: 3.4,
      selected: false,
      total: 0
    }, {
      id: 13,
      imaUrl: "../../static/recommend/推荐13.jpg",
      disc: "洛丽塔深红下午茶套装",
      price: 106,
      collect: 9.9,
      selected: false,
      total: 0
    }, {
      id: 14,
      imaUrl: "../../static/recommend/推荐14.jpg",
      disc: "孔雀绿开叉优美服 沙滩 方便 优雅",
      price: 166,
      collect: 14.5,
      selected: false,
      total: 0
    }, {
      id: 15,
      imaUrl: "../../static/recommend/推荐15.jpg",
      disc: "运动长裤",
      price: 206,
      collect: 16.4,
      selected: false,
      total: 0
    }, {
      id: 16,
      imaUrl: "../../static/recommend/推荐16.jpg",
      disc: "黑白配雪顶咖啡折扣券",
      price: 306,
      collect: 12.2,
      selected: false,
      total: 0
    }, ],
  },
  // 左上角分类页面的导航
  goTo: function(e) {
    wx.navigateTo({
      url: "../sorts/sorts",
    })
  },
  // 点击商品后进入到了商品详细页
  goToDetail: function(e) {
    let ima = e.currentTarget.dataset.ima;
    let disc = e.currentTarget.dataset.disc;
    let price = e.currentTarget.dataset.price;
    let collect = e.currentTarget.dataset.col;
    let id = e.currentTarget.dataset.id;
    let argu = "?ima=" + ima + "&disc=" + disc + "&price=" + price + "&collect=" + collect + "&id=" + id;
    wx.navigateTo({
      url: '/pages/goodsDetail/goodsDetail' + argu,
    })
  },
  // 点击将商品添加到购物车中
  addToCar: function(e) {
    // 获取全局的data对象
    let app = getApp();
    // 如果没登录就显示错误
    let res = wx.getStorageSync('currentUser');
    if (!res) {
      wx.showToast({
        title: '请先登录哦~',
        icon: "none",
        success() {
          wx.setStorageSync('hasAddGoods', {})
        }
      })
      return;
    }
    // 将页面点击后传过来的id保存
    let addId = e.currentTarget.dataset.id;
    // 保存已经添加到购物车的数组
    let carLength = app.data.hasAddToCarList.length;
    // 判断购物车里是否已经有了该商品
    for (let i of app.data.hasAddToCarList) {
      if (addId == i.id) {
        wx.showToast({
          title: '已经在购物车了哦~',
          icon: "none"
        })
        return;
      }
    }
    // 将商品添加进购物车
    for (let i of app.data.shuJuKuList) {
      if (addId == i.id) {
        app.data.hasAddToCarList.push(i);
        wx.setStorage({
          data: app.data.hasAddToCarList,
          key: 'hasAddGoods',
        })
      }
    }
    // 通过数组长度判断购物车是否已经添加好了
    if (app.data.hasAddToCarList.length > carLength) {
      wx.showToast({
        title: '已添加~',
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})